package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.BankCardMapper;
import com.jd.finance.core.pojo.entity.BankCard;
import com.jd.finance.core.service.BankCardService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.RegexValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 银行卡表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-17
 */
@Service
public class BankCardServiceImpl extends ServiceImpl<BankCardMapper, BankCard> implements BankCardService {

    @Resource
    private BankCardMapper bankCardMapper;

    /**
     * 提交银行卡信息
     *
     * @param uid 用户id
     * @param bid 银行id
     * @param cardNumber 银行卡号
     * @return
     */
    @Override
    public BaseResult submitCardInfo(Integer uid, Integer bid, String cardNumber) {

        if (!StringUtils.isAllBlank(String.valueOf(uid), String.valueOf(bid), cardNumber)) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }
        boolean checkCardNum = RegexValidateUtils.checkCardNumber(cardNumber);
        if (!checkCardNum) {
            log.error("银行卡格式不正确！");
            return BaseResult.error().message("银行卡号格式不正确!");
        }
        BankCard bankCard = new BankCard();
        bankCard.setCardNumber(cardNumber);
        bankCard.setUid(uid);
        int count = bankCardMapper.insert(bankCard);
        if (count == 0) {
            log.error("提交信息失败！");
            return BaseResult.error().message("提交信息失败!");
        }
        return BaseResult.ok().data("tijiaoResult", 1);
    }

    /**
     * 根据uid查询银行卡信息
     * @param uid
     * @return
     */
    @Override
    public BaseResult getCardInfo(Integer uid) {
        if (uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }

        List<BankCard> list = bankCardMapper.selectBankCardInfo(uid);
        if (list == null || list.size() == 0){
            log.error("未查询到银行卡信息!");
            return BaseResult.error().message("未查询到银行卡信息!");
        }
        return BaseResult.ok().data("bank",list);
    }

}
